
const { readFile } = require('./File');
const help = require('./help');

const blankSpace = '\xa0';
const enter = '\n';
const line = `${enter}${blankSpace.repeat(5)}\\${enter}${blankSpace.repeat(6)}\\${enter}`;

const strFn = (text) => {
   return text.replaceAll('\\n', '\n');
}

const getColor = (color) => {
    const { sayColor, graphicsColor } = JSON.parse(color);
    const SC = sayColor.split('#')[0];
    const GC = graphicsColor.split('#')[0];
    return { SC, GC }
}

module.exports = async (say) => {
    try {
        say = say ?? '';
        const name = await readFile('../data/graphics.txt');
        const [figure, color] = await Promise.all([
            readFile(`../figure/${name}.txt`),
            readFile('../data/color.txt')
        ]);
        const { SC, GC } = getColor(color);
        const text = `\u001b[${SC}m${strFn(say)}\u001b[0m`;
        const image = `\u001b[${GC}m${figure}\u001b[0m`;
        const show = say.length > 0 ? `${text}${line}${image}` : `${image}`;
        console.log(show);
    } catch (err) {
        switch (err.errno) {
            case -2:
                help();
                break
            default:
                console.log(err);
        }
    }
}
